import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

// 拦截
router.beforeEach(async (to, from, next) => {
	const token = localStorage.getItem('token')
	const role = localStorage.getItem('role');

	const whiteList = ['/login', '/register']

	if (token) {
		// 已登录
		// 跳转登录或者注册
		if (whiteList.includes(to.path)) {
			// 直接首页
			next({ path: '/home' })
		} else {
			// 没有动态路由
			if (store.state.addRouter.length === 0) {
				// 生成动态路由
				await store.dispatch('set_dynamicRouter', role)
				next({ ...to, replace: true })
			} else {
				// 说明已经生成动态路由表
				next()
			}
		}


	} else {
		// 未登录
		if (whiteList.includes(to.path)) {
			next()
		} else {
			next({ path: '/login' })
		}
	}

})




createApp(App).use(store).use(router).use(ElementPlus).mount('#app')
